package com.service.impl;

import com.pojo.AreaPojo;
import com.service.IAreaService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AreaService implements IAreaService {
    private static List<AreaPojo> allData = new ArrayList<>();
    static {
        allData.add(new AreaPojo(null, "00", "0", "陕西省"));
        allData.add(new AreaPojo("00", "0000", "1", "西安市"));
        allData.add(new AreaPojo("0000", "000000", "2", "碑林区"));
        allData.add(new AreaPojo("0000", "000001", "2", "未央区"));
        allData.add(new AreaPojo("00", "0001", "1", "宝鸡市"));
        allData.add(new AreaPojo("0001", "000100", "2", "健康区"));
        allData.add(new AreaPojo("0001", "000101", "2", "如意区"));

        allData.add(new AreaPojo(null, "01", "0", "哈哈省"));
        allData.add(new AreaPojo("01", "0100", "1", "嘿嘿市"));
        allData.add(new AreaPojo("0100", "010000", "2", "嘻嘻区"));
        allData.add(new AreaPojo("0100", "010001", "2", "略略区"));
        allData.add(new AreaPojo("01", "0101", "1", "呜呜市"));
        allData.add(new AreaPojo("0101", "010100", "2", "莱莱区"));
        allData.add(new AreaPojo("0101", "010101", "2", "呵呵区"));
    }


    @Override
    public List<AreaPojo> queryAllProvinces() {
        List<AreaPojo> collect = allData.stream().filter(a -> "0".equals(a.getCodeType())).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<AreaPojo> queryAreaByParentCode(String code) {
        List<AreaPojo> collect = allData.stream().filter(a -> code.equals(a.getParentCode())).collect(Collectors.toList());
        return collect;
    }

    @Override
    public AreaPojo queryAreaByCode(String code) {
        AreaPojo areaPojo = null;
        List<AreaPojo> collect = allData.stream().filter(a -> code.equals(a.getCode())).collect(Collectors.toList());
        if(collect != null && collect.size() > 0){
            areaPojo = collect.get(0);
        }else{
            areaPojo = new AreaPojo();
        }
        return areaPojo;
    }

    @Override
    public void saveArea(String name, String parentCode) {
        List<AreaPojo> collect = allData.stream().filter(a -> parentCode.equals(a.getParentCode())).collect(Collectors.toList());
        String code = collect.get(collect.size() - 1).getCode().substring(0, collect.size() - 2)+collect.size();
        String codeType = "1";
        if(parentCode.length()>2){
            codeType = "2";
        }
        allData.add(new AreaPojo(parentCode, code, codeType, name));
    }
}
